import sql from '@/utils/sqlLite.js'
import {
	setObjSql,
	obj2str_sql
} from '@/utils/index.js'
export const database = uni.getStorageSync('database')
// 数据表名称
export const lease = 'lease'
// 用户数据表
/*


nickname 昵称
phone 手机
avatar 头像
rentNum 租赁次数
overdueNum 逾期次数
productModel 品类
productId 
userAddress 住址

deviceTypeName 产品类型名称
deviceId 产品Id
photo 产品品类图片
bondsMan 担保人姓名
bondsManPhone 担保人电话
remind 提醒次数 超出三次不让用户操作
leaseDay 租赁天数 1天 2天
leasePrice 租赁价格
expirationTime 到期时间
leaseStatus 租赁状态   1在库、2预定、4租赁中、6逾期、8丢失、10故障
history 历史记录
overdueDay 逾期时间 1H 逾期多久
leaseId 租赁Id 生成的当前时间戳
nextPopupTime 下次弹框时间 逾期第一次是和到期时间一样，填写一次记录之后往后面加一个小时 做到一小时后提醒一次
lastTimeRent 上次的租赁时间
photo 设备图片
rentCode TR+YYYYMMDD+八位随机数
overTime:逾期的创建时间
rentDtos   {  租赁信息
	createTime 创建时间
 rentInitProperty:{ 设备属性 主要是设备返回的信息
	 
 }	 
}
backDtos { //归还信息
	createTime 创建时间
	backProperty:{ 设备属性
		 
	}	
	
},
currProp:{ 设备的最新属性
	
}
counter 计数器
iSync 是否同步 1同步完成 0 未同步
agencySecurityDeposit 押金
*/
// 创建数据表
export const crateLease = async () => {
	let sqltxt = [
		`create table if not exists  ${lease}
		("customerAccount" VARCHAR(16),
		"nickname" VARCHAR(16),
		"phone" VARCHAR(24),
		"avatar" VARCHAR(128),
		"rentNum" int(4),
		"overdueNum" int(4),
		"productModel" VARCHAR(16),
		"productId" VARCHAR(16),
		"userAddress" VARCHAR(64),
		"bondsMan" VARCHAR(16),
		"bondsManPhone" VARCHAR(24),
		"remind" int(1),
		"leaseDay" int(2),
		"leasePrice" int(4),
		"expirationTime" VARCHAR(16),
		"leaseStatus" int(1),
		"history" VARCHAR(512),
		"overdueDay" int(2),
		"leaseId" VARCHAR(16),
		"nextPopupTime" VARCHAR(16),
		"lastTimeRent" VARCHAR(16),
		"photo" VARCHAR(128),
		"rentCode" VARCHAR(24),
		"rentDtos" VARCHAR(512),
		"backDtos" VARCHAR(512),
		"overTime" VARCHAR(16),
		"currProp" VARCHAR(512),
		"counter" init(5),
		"iSync" init(1),
		"agencySecurityDeposit" VARCHAR(8))`,
	];
	let res = await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
}
// 租灯前 判断该设备是否处于租赁中
export const getlease4 = async (productId) => {

	let sqltxt = `select *from ${lease} where productId = '${productId}' and leaseStatus = 4`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	
}

// 添加租赁数据
export const addLease = async (pushData) => {
	// 数据库中id 存在 修改 ，不存在 新增
	let sqltxt = `select *from ${lease} where leaseId = '${pushData.leaseId}'`;
	let res = await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	if (res.code == 0 && res.data.length == 0) { //数据库中没有数据 新增

		let condition = Object.keys(pushData).join(',')
		let data = setObjSql(pushData)
		let sqltxt = `INSERT  INTO ${lease} (${condition}) VALUES(${data})`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	} else { //存在 ，更改

		let data = obj2str_sql(pushData)
		let sqltxt = `update  ${lease} set ${data} where leaseId = '${pushData.leaseId}'`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	}
}

// 删除租赁 leaseId 
export const removeLease = async (leaseId) => {
	let sqltxt = [
		`delete from  ${lease}  where leaseId = '${leaseId}'`
	];
	return sql.executeSQL(uni.getStorageSync('database'), sqltxt);

}
// 获取全部的租赁数据
export const getLease = async () => {
	let sqltxt = `select * from ${lease}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}
// 获取未同步的数据
export const getNoSyncLease = async () => {
	let sqltxt = `select * from ${lease} where iSync = 0`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}

// 根据produceId 查找 租赁列表
export const leaPid = async (productId) => {
	let sqltxt = `select * from ${lease} where productId = ${productId} and leaseStatus = 4`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}
// 查询非在库设备 租赁中或者是逾期的设备,10.1,增加丢失也可还
export const leaNoStore = async (productId) =>{
	let sqltxt = `select * from ${lease} where productId = ${productId} and (leaseStatus = 4 or leaseStatus = 6 or leaseStatus = 8) ORDER BY leaseId DESC limit 1`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	
}

export const leaFaultStore = async (productId) =>{
	let sqltxt = `select * from ${lease} where productId = ${productId} and (leaseStatus = 4 or leaseStatus = 6) ORDER BY leaseId DESC limit 1`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	
}

export const getLeaseToId = async (leaseId) => {
	let sqltxt = `select * from ${lease} where leaseId = ${leaseId}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}

export const setSyncDate = async (productId) => {
	let sqltxt = ` UPDATE ${lease} SET iSync=REPLACE(iSync,'0','1') where productId = ${productId}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}

export const delLease = async () => {
	let sqltxt = `delete  from ${lease} `;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

// 删除某一条
export const delLeasePid = async (productId) => {
	let sqltxt = `delete  from ${lease} where productId = '${productId}' `;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}